import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.event.*;
import javax.swing.*;
import java.awt.*;
import java.util.ArrayList;


class StringPanel extends JPanel implements ActionListener{
	public String wordData, colorData, selectedColor;
	int i = 0;
	public static ArrayList<SearchList> searchHistory = 
		new ArrayList<SearchList>();
	public SearchList element;
	
	private JTextField stringField;
	private JLabel	stringLabel,
					colorLabel;
	private JButton closeButton,
					searchButton;
	private String[] colors = {"", "red", "blue", "green", "orange", "pink"};
	private JComboBox colorsComboBox;
	
	
	public StringPanel(){
		JPanel displayPanel = new JPanel();
		displayPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
		stringLabel = new JLabel("String:                       ");
		displayPanel.add(stringLabel);
		stringField = new JTextField(20);
		displayPanel.add(stringField);
		colorLabel = new JLabel("Color:                        ");
		displayPanel.add(colorLabel);
		JPanel buttonPanel = new JPanel();
		buttonPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));

		colorsComboBox = new JComboBox(colors);
		colorsComboBox.addActionListener(this);
		displayPanel.add(colorsComboBox, getConstraints(1, 2, 1, 1, GridBagConstraints.WEST));
		
		searchButton = new JButton("Search");
		searchButton.addActionListener(this);
		buttonPanel.add(searchButton);
		
		closeButton = new JButton("Close");
		closeButton.addActionListener(this);
		buttonPanel.add(closeButton);
		
		this.setLayout(new BorderLayout());
		this.add(displayPanel, BorderLayout.CENTER);
		this.add(buttonPanel, BorderLayout.SOUTH);
		
	}
	
	public void modifyFields(){
		wordData = stringField.getText();
		colorData = selectedColor;
		this.element = new SearchList(wordData, colorData, "");
		searchHistory.add(i++, element);
		
	}
	
	public void actionPerformed(ActionEvent e){
		Object source = e.getSource();
		if (source == closeButton){
			printList();
			JFrame currentFrame = (JFrame) getTopLevelAncestor();
			currentFrame.dispose();
			System.exit(0);
		}
		else if (source == searchButton){
			modifyFields();
			searchButton.setText("Clicked!");
		}
		else if (source == colorsComboBox){
			/* problem if you don't click on the first one, which is showing when window pops-up
			 * it saves null in the list so I added empty string to it too
			 */
			int i = colorsComboBox.getSelectedIndex();
			selectedColor = colors[i].toString();
			
		}
	}
	
	private GridBagConstraints getConstraints(int gridx, int gridy, int gridwidth,
											int gridheight, int anchor){
		GridBagConstraints c = new GridBagConstraints();
		c.insets = new Insets(5, 5, 5, 5);
		c.ipadx = 0;
		c.ipady = 0;
		c.gridx = gridx;
		c.gridy = gridy;
		c.gridwidth = gridwidth;
		c.gridheight = gridheight;
		c.anchor = anchor;
		return c;
	}
	
	public void printList(){
		//planing to add a button for optional drop down of already searched strings 
		//	and a box for optional notes
		for (int i=0; i<searchHistory.size();i++){
			SearchList x = searchHistory.get(i);
			System.out.println(x.toString());
		}
	}
}
